package com.apchernykh.types;

import org.w3c.dom.Node;
import org.w3c.dom.Element;

import android.os.Parcel;
import android.os.Parcelable;
import com.apchernykh.rozhlasapp.utils.ConditionalLog;
import com.apchernykh.rozhlasapp.utils.XMLParsingHelper;

public class StationInfo implements Parcelable {
	public String mShortName = "Short Name Unavailable";
	public String mCategory = "Category Unavailable";
	public String mShortdescription = "";
	public OnGoingInfoURL mOnGoing = new OnGoingInfoURL();
	public StreamURLs mp3URL = new StreamURLs();
	public RadioSchedule mPlayingNow = null;
	public RadioSchedule mNextPrograms = null;
	public long mExpirationTime = 0;
	public int mStationId = 0;

	@Override
	public void writeToParcel(Parcel oParcel, int iUnusedFlags) {
		oParcel.writeString(mShortName);
		oParcel.writeString(mCategory);
		oParcel.writeInt(mStationId);
		oParcel.writeLong(mExpirationTime);
		oParcel.writeParcelable(mOnGoing, 0);
		oParcel.writeParcelable(mPlayingNow, 0);
		oParcel.writeParcelable(mNextPrograms, 0);
		oParcel.writeParcelable(mp3URL, 0);
	}

	public StationInfo(Parcel iParcel) {
		this.mShortName = iParcel.readString();
		this.mCategory = iParcel.readString();
		this.mStationId = iParcel.readInt();
		this.mExpirationTime = iParcel.readLong();
		this.mOnGoing = iParcel.readParcelable(OnGoingInfoURL.class.getClassLoader());
		this.mPlayingNow = iParcel.readParcelable(RadioSchedule.class
				.getClassLoader());
		this.mNextPrograms = iParcel.readParcelable(RadioSchedule.class
				.getClassLoader());
		this.mp3URL = iParcel.readParcelable(StreamURLs.class.getClassLoader());
	}

	public static final Parcelable.Creator<StationInfo> CREATOR = new Parcelable.Creator<StationInfo>() {
		public StationInfo createFromParcel(Parcel iParcel) {
			return new StationInfo(iParcel);
		}

		public StationInfo[] newArray(int iSize) {
			return new StationInfo[iSize];
		}
	};

	public StationInfo(String iStationShortName, String iLowBitrateUri, String iHighBitrateUri) {
		this.mShortName = iStationShortName;
		mp3URL = new StreamURLs(iLowBitrateUri, iHighBitrateUri);
	}

	public StationInfo(Node iNode) {
		try {
			mShortName = XMLParsingHelper.extractChildText(iNode, "shortname");
			mCategory = ((Element) iNode).getAttribute("type");
			mShortdescription = XMLParsingHelper.extractChildText(iNode,
					"shortdescription");
			mOnGoing.refresh(iNode);
			mp3URL.refresh(iNode);
		} catch (Throwable e) {
			ConditionalLog.e("could not read station description: "
					+ mShortName + " " + iNode.getLocalName());
		}
		if (mp3URL != null && mp3URL.mBRs.size() == 0)
			mp3URL = null;
	}

	public RadioSchedule getPlayingNowInfo() {
		return mPlayingNow;
	}

	public RadioSchedule refreshNextInfo() {
		if (mNextPrograms == null) {
			mNextPrograms = new RadioSchedule(mOnGoing.mNext);
		}
		mNextPrograms.refresh();
		return mNextPrograms;
	}

	public void forceRefresh()
	{
		refreshPlayingNowInfo();
		refreshNextInfo();
		resetExpirationTime();
	}
	
	public void refreshInfo() {
		if (System.currentTimeMillis() > mExpirationTime) {
			forceRefresh();
		}
		ApplicationModel.notifyViewExpirationTime(mExpirationTime);
	}
	final static long aServerLatencyGap = 120000;
	final static long aPollTimeout = 15000;

	public void resetExpirationTime() {
		long aCurrentTime = System.currentTimeMillis();
		if (mPlayingNow.mPrograms != null && mPlayingNow.mPrograms.size() > 0) {
			mExpirationTime = mPlayingNow.mPrograms.get(0).mStopTime;
			if (mExpirationTime < aCurrentTime
					&& (aCurrentTime - mExpirationTime) < aServerLatencyGap) {
				mExpirationTime = aCurrentTime + aPollTimeout;
				//ConditionalLog.e("enabled polling for station "+ mStationId);
				// Sometimes, Schedule is not updated on the rozhlas server
				// immediately. We should respect some latency here
			}
		} else {
			// default value
			mExpirationTime = aCurrentTime + 300000;
		}
	}

	private RadioSchedule refreshPlayingNowInfo() {
		if (mPlayingNow == null) {
			mPlayingNow = new RadioSchedule(mOnGoing.mNow);
		}
		mPlayingNow.refresh();
		return mPlayingNow;
	}

	public void copy(StationInfo iSource) {
		mExpirationTime = iSource.mExpirationTime;
		mStationId = iSource.mStationId;
		mNextPrograms = iSource.mNextPrograms;
		mPlayingNow = iSource.mPlayingNow;
		mOnGoing = iSource.mOnGoing;
		mp3URL = iSource.mp3URL;
		mCategory = iSource.mCategory;
		mShortdescription = iSource.mShortdescription;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public String getStationSummaryNotif() {
		return mShortName;
	}

	public String getPlayingNowSummaryNotif() {
		if (mPlayingNow != null && mPlayingNow.mPrograms.size() > 0
				&& mPlayingNow.mPrograms.get(0) != null) {
			return mPlayingNow.mPrograms.get(0).mName;
		}
		return null;
	}

}
